package com.cxyd.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxyd.common.page.PageData;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.common.utils.ConvertUtils;
import com.cxyd.common.utils.ProcResult;
import com.cxyd.modules.api.dto.StockDetailDTO;
import com.cxyd.modules.api.entity.StockDetailEntity;
import com.cxyd.modules.security.user.SecurityUser;
import com.cxyd.modules.sys.dto.SysUserDTO;
import com.cxyd.modules.sys.service.SysUserService;
import com.cxyd.modules.wms.dao.*;
import com.cxyd.modules.wms.dto.StockDTO;
import com.cxyd.modules.wms.dto.StockExpiryDTO;
import com.cxyd.modules.wms.dto.StockOverdueDTO;
import com.cxyd.modules.wms.entity.*;
import com.cxyd.modules.wms.service.StockOverdueService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 库存信息
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@Service
public class StockOverdueServiceImpl extends CrudServiceImpl<StockOverdueDao, StockOverdueEntity, StockOverdueDTO> implements StockOverdueService {

    private final SysUserService userService;

    public StockOverdueServiceImpl(SysUserService userService) {
        this.userService = userService;
    }

    @Override
    public PageData<StockOverdueDTO> page(Map<String, Object> params) {
        //1.查询库存信息
        final IPage<StockOverdueEntity> page = baseDao.selectPage(
                getPage(params, null, true),
                getWrapper(params)
        );
        //2.获取用户列表
        final List<SysUserDTO> userList = userService.getList();
        //3.将creator与updater的username通过id进行绑定
        page.getRecords().forEach(entity -> {
            if (entity.getCreator() != null) {
                entity.setCreatorName(userService.getUsernameById(entity.getCreator(), userList));
            }
            if (entity.getUpdater() != null) {
                entity.setUpdaterName(userService.getUsernameById(entity.getUpdater(), userList));
            }
        });
        return getPageData(page, StockOverdueDTO.class);
    }

    @Override
    public QueryWrapper<StockOverdueEntity> getWrapper(Map<String, Object> params) {
        //查询条件
        String spuNo = (String) params.get("spuNo");
        String spuName = (String) params.get("spuName");
        String contNo = (String) params.get("contNo");
        String reelId = (String) params.get("reelId");
        String curPosition = (String) params.get("curPosition");
        String positionType = (String) params.get("positionType");

        QueryWrapper<StockOverdueEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(positionType), "position_type", positionType);
        wrapper.like(StringUtils.isNotBlank(contNo), "cont_no", contNo);
        wrapper.like(StringUtils.isNotBlank(reelId), "reel_id", reelId);
        wrapper.like(StringUtils.isNotBlank(spuNo), "spu_no", spuNo);
        wrapper.like(StringUtils.isNotBlank(spuName), "spu_name", spuName);
        wrapper.like(StringUtils.isNotBlank(curPosition), "cur_position", curPosition);

        wrapper.orderByDesc("over_days");
        wrapper.orderByAsc("spu_no");

        return wrapper;
    }

    @Override
    public List<StockOverdueDTO> getList(Map<String, Object> params) {
        //1.查询库存信息
        final List<StockOverdueEntity> page = baseDao.selectList(
                getWrapper(params)
        );
        //2.获取用户列表
        final List<SysUserDTO> userList = userService.getList();
        //3.将creator与updater的username通过id进行绑定
        page.forEach(entity -> {
            if (entity.getCreator() != null) {
                entity.setCreatorName(userService.getUsernameById(entity.getCreator(), userList));
            }
            if (entity.getUpdater() != null) {
                entity.setUpdaterName(userService.getUsernameById(entity.getUpdater(), userList));
            }
        });
        return ConvertUtils.sourceToTarget(page, StockOverdueDTO.class);
    }

}